package org.school.controller;

import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.school.entity.*;
import org.school.service.UserService;
import org.school.staticException.PasswordIncorrectException;
import org.school.staticException.UserNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * (User)表控制层
 *
 * @author makejava
 * @since 2022-12-29 00:52:13
 */
@RestController
@RequestMapping("user")
@CrossOrigin
@Log4j2
public class UserController {
    /**
     * 服务对象
     */
    @Resource
    private UserService userService;
    @Autowired
    private StringRedisTemplate redisTemplate;



    /**
     * 分页查询
     *
     * @param user 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<User>> queryByPage(User user, PageRequest pageRequest) {
        return ResponseEntity.ok(this.userService.queryByPage(user, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<User> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param user 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<User> add(User user) {
        return ResponseEntity.ok(this.userService.insert(user));
    }

    /**
     * 编辑数据
     *
     * @param user 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<User> edit(User user) {
        return ResponseEntity.ok(this.userService.update(user));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.userService.deleteById(id));
    }
@PostMapping("getdd")
public ResponseEntity login (@RequestBody @Validated Dingdan dd, BindingResult bindingResult){//下单功能

if(dd.getHost()==null||dd.getUser()==null){//如果没有订单用户或订单的商家
    return   ResponseEntity.status(500).build();
}
    message2 mes=new message2("创建订单失败",0);
int i=userService.order(dd);//返回1就是成功的
    if(i==1){
        mes.setStatus(1);
        mes.setContent("创建订单成功");
    }
    return ResponseEntity.ok().body(mes);
}
@GetMapping("getOrderByUser")//用户查看自己的订单
public ResponseEntity<List<Dingdan>> getOrder(String name) {
    return ResponseEntity.ok().body( this.userService.querryOrderByUser(name));
}
@PutMapping("completeByUser")//用户收到订单
public ResponseEntity comByUser(String name,int id) {
        Date date=new Date();
        String staus="已完成";
    if(this.userService.completeOrderByUser(name,date,staus,id)==-1){//验证
        return  ResponseEntity.status(500).build();
    }
    return ResponseEntity.ok().body( this.userService.completeOrderByUser(name,date,staus,id));
}
    @GetMapping("shop")//使用分页查询所有商店
    public ResponseEntity<PageInfo<Shop>> getShopByPage( int pageNum, int size ) {

        return ResponseEntity.ok(this.userService.findShopInfo(pageNum, size));
    }
    @GetMapping("slecetshop")//模糊分页查询店铺
    public ResponseEntity<PageInfo<Shop>> getNoClearlyShopByPage( String name,int pageNum, int size ) {

        return ResponseEntity.ok(this.userService.findListShopInfo(name,pageNum, size));
    }
    @GetMapping("info")//通过token获取user的信息
    public ResponseEntity getInfoByToken(@RequestHeader("token") String token){
        System.out.println("获取用户信息中");
if(token==null||!redisTemplate.hasKey(token)){
    System.out.println("获取失败");
    System.out.println(token);
    return ResponseEntity.status(404).body("error:当前token不是有效值");
}else {
    String userinfo=redisTemplate.opsForValue().get(token);
    System.out.println("获取成功");
    return ResponseEntity.ok().body(userinfo);
}
    }

}

